package com.socodethis.spaceex.math;

import java.util.Random;

import com.badlogic.gdx.math.Rectangle;
import com.socodethis.spaceex.math.cPoint;

public class cRect {
	private cPoint position, dimensions;
	Random random = new Random(System.nanoTime());
	int switcher;
	public cRect(cRect rect){
		this(rect.position.getX(),rect.position.getY(),
				rect.dimensions.getX(),rect.dimensions.getY());
	}
	public cRect(cPoint a, cPoint b, int minimumDimensions){
		//Use a.getX() if x of pos
		if(a.getX() < b.getX()){
			if(a.getY() < b.getY()){
				position = new cPoint(a);
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),Math.abs(a.getY()-b.getY()));
			}else if(a.getY() > b.getY()){
				position = new cPoint(a.getX(), b.getY());
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),Math.abs(a.getY()-b.getY()));
			}else{
				position = new cPoint(a.getX(), a.getY());
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),minimumDimensions);
			}
		}else if(a.getX() > b.getX()){
			if(a.getY() > b.getY()){
				position = new cPoint(b);
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),Math.abs(a.getY()-b.getY()));
			}else if(a.getY() < b.getY()){
				position = new cPoint(b.getX(), a.getY());
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),Math.abs(a.getY()-b.getY()));
			}else{
				position = new cPoint(b.getX(), b.getY());
				dimensions = new cPoint(Math.abs(a.getX()-b.getX()),minimumDimensions);
			}
			//X coords are equal
		}else{
			if(a.getY() > b.getY()){
				position = new cPoint(b);
				dimensions = new cPoint(minimumDimensions,Math.abs(a.getY()-b.getY()));
			}else if(a.getY() < b.getY()){
				position = new cPoint(a);
				dimensions = new cPoint(minimumDimensions,Math.abs(a.getY()-b.getY()));
			}else{
				position = new cPoint(a);
				dimensions = new cPoint(minimumDimensions,minimumDimensions);
			}
		}
		if(dimensions.getX() < minimumDimensions){
			dimensions.setPoint(minimumDimensions, dimensions.getY());
		}
		if(dimensions.getY() < minimumDimensions){
			dimensions.setPoint(dimensions.getX(), minimumDimensions);
		}
	}
	public cRect(float x,float y, float width, float height){
		position = new cPoint(x,y);
		dimensions = new cPoint(width,height);
	}
	public boolean withinSquare(cPoint point){
		if(point.getX()>position.getX() && point.getX()<position.getX()+dimensions.getX()&&
				point.getY()>position.getY() && point.getY()<position.getY()+dimensions.getY()){
			return true;
		}
		return false;
	}
	public boolean inSquare(cPoint point){
		if(point.getX()>=position.getX() && point.getX()<=position.getX()+dimensions.getX()&&
				point.getY()>=position.getY() && point.getY()<=position.getY()+dimensions.getY()){
			return true;
		}
		return false;
	}
	public boolean onSquareBounds(cPoint point){
		if((point.getX() >= position.getX() && point.getX()<=position.getX()+dimensions.getX() && (point.getY()==position.getY() || point.getY()==position.getY()+dimensions.getY()))||
				((point.getX() == position.getX() || point.getX()==position.getX()+dimensions.getX()) && point.getY()>=position.getY() && point.getY()<=position.getY()+dimensions.getY())){
			return true;
		}
		return false;
	}
	public void addWidth(int increment){
		this.position.setPoint(position.getX()-increment, position.getY());
		this.dimensions.setPoint(dimensions.getX()+(2*increment), dimensions.getY());
	}
	public void addHeight(int increment){
		this.position.setPoint(position.getX(), position.getY()-increment);
		this.dimensions.setPoint(dimensions.getX(), dimensions.getY()+(2*increment));
	}
	public cPoint getPosition(){
		return this.position;
	}
	public cPoint getDimensions(){
		return this.dimensions;
	}
	public boolean overlaps (cRect rectangle) {
		return !(position.getX() >= rectangle.position.getX() + rectangle.dimensions.getX() || position.getX() + dimensions.getX() <= rectangle.position.getX() 
				|| position.getY() >= rectangle.position.getY() + rectangle.dimensions.getY() || position.getY() + dimensions.getY() <= rectangle.position.getY());
	}
	public boolean containsRect(cRect rectangle) {
		float xmin = rectangle.position.getX();
		float xmax = xmin + rectangle.dimensions.getX();

		float ymin = rectangle.position.getY();
		float ymax = ymin + rectangle.dimensions.getY();

		return ((xmin >= position.getX() && xmin <= position.getX() + dimensions.getX()) && (xmax >= position.getX() && xmax <= position.getX() + dimensions.getX()))
			&& ((ymin >= position.getY() && ymin <= position.getY() + dimensions.getY()) && (ymax >= position.getY() && ymax <= position.getY() + dimensions.getY()));
	}
	public cRect containInRect(cRect rect){
		cRect rect2 = new cRect(rect);
		
		if(rect2.position.getX()<= this.position.getX()){
			rect2.position.setPoint(this.position.getX(), rect2.position.getY());
		}
		if(rect2.position.getY()<= this.position.getY()){
			rect2.position.setPoint(rect2.position.getX(), this.position.getY());
		}
		if(rect2.position.getX()+rect2.dimensions.getX()>= this.position.getX()+this.dimensions.getX()){
			rect2.dimensions.setPoint(this.dimensions.getX(), rect2.dimensions.getY());
		}
		if(rect2.position.getY()+rect2.dimensions.getY()>= this.position.getY()+this.dimensions.getY()){
			rect2.dimensions.setPoint(rect2.dimensions.getX(), this.dimensions.getY());
		}
		
		return rect2;
	}
	public cPoint getRandomEdge(){
		switcher = random.nextInt( 4 );
		switch(switcher){
			//Up
			case 0:
				return new cPoint(position.getX()+random.nextInt( (int) dimensions.getX()-2 )+1,position.getY());
			//Down
			case 1:
				return new cPoint(position.getX()+random.nextInt( (int) dimensions.getX()-2 )+1,position.getY()+dimensions.getY());
			//Left
			case 2:
				return new cPoint(position.getX(),position.getY()+ random.nextInt( (int) dimensions.getY()-2 )+1);
			//Right
			case 3:
				return new cPoint(position.getX()+dimensions.getX(),position.getY()+ random.nextInt( (int) dimensions.getY()-2 )+1);
			default:return new cPoint(position.getX()+random.nextInt( (int) dimensions.getX()-2 )+1,position.getY());
		}
	}
	public void printDimensions(){
		System.out.println("Position: " + position.getX() + ":" + position.getY());
		System.out.println("Dimensions: " + dimensions.getX() + ":" + dimensions.getY());
	}
}
